<template>
	<view>
		<ikz-loading :css-data="loading_css"></ikz-loading>
		<form @submit="formSubmit" class="main">
			<view class="ikz-add">
				<view class="addRec-menu-o">
					<view class="addRec-menu-o-left">收件人</view>
					<view class="addRec-menu-o-right">
						<input name="name" placeholder="请输入收件人姓名" cursor-spacing="5" value="" />
					</view>
				</view>
				<view class="addRec-menu-o">
					<view class="addRec-menu-o-left">联系电话</view>
					<view class="addRec-menu-o-right">
						<input name="mobile" type="number" placeholder="请输入联系电话" cursor-spacing="5" value="" />
					</view>
				</view>
				<view class="addRec-menu-o">
					<view class="addRec-menu-o-left">收货地址</view>
					<view class="addRec-menu-o-right" style="background: #EDEFF2;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		padding: 0 26rpx;
		box-sizing: border-box;" @click="geselectAddress">
						<view style="color: #828282;">
							{{group_name}}
						</view>
						<view class="ikz-cell-ft-in-access">
						</view>
						<!-- <input name="address" placeholder="" value="" style="height:60rpx;" cursor-spacing="5" /> -->
					</view>
				</view>
				<view class="addRec-menu-o">
					<view class="addRec-menu-o-left">详细地址</view>
					<view class="addRec-menu-o-right">
						<input name="detail" placeholder="请输入详细地址 例:3层302室" value="" />
					</view>
				</view>
			</view>

			<view class="moren">
				<view class="moren_txt">设为默认地址</view>
				<view style="margin-left:10%;">
					<view class="addRec-menu-o-right">
						<switch name="defaults" color="#07C160" @change="switchChange" />
					</view>
				</view>
			</view>

			<!-- 地址说明 -->
			<view class="tips-txt">
				<view class="top-txt">
					<image src="/static/address-icon.png" style="width: 52rpx;height:48rpx;" mode=""></image>
					<view class="sayming">
						地址说明
					</view>
				</view>
				<view class="center-txt">
					提示说明后台添加提示说明后台添加
				</view>
			</view>
			<view class="bot-box">
				<button class="ikz-add-address-btn" form-type="submit">添加地址</button>
			</view>
		</form>
	</view>
</template>

<script>
	import ikz from '@/pages/Common/tools';
	import ikzLoading from '@/pages/Component/Public/Loading/loading.vue';
	var app = getApp( );
	export default {
		components: {
			ikzLoading
		},
		data( ) {
			return {
				local: '', //具体地址 值  
				defaults: false, // 默认地址
				current_addr: {}, //点击获取的用户信息
				color: '',
				loading_css: {
					display: 'flex'
				},
				group_name: '请选择地址/楼栋'
			};
		},
		onLoad: function( options ) {},
		onShow: function( ) {
			this.loading_css.display = 'none';
			var group_name = app.globalData.campus_data;
			if ( group_name ) {
				this.group_name = group_name;
			} else {
				this.group_name = '请选择地址/楼栋';
			}

		},
		methods: {
			geselectAddress( ) {
				app.globalData.campus_data = '';
				uni.navigateTo( {
					url: '/pages/Component/Module/Address/Program/selectAddress/index'
				} )
			},
			//新增地址提交
			formSubmit: function( e ) {
				if ( !uni.getStorageSync( 'local_user_session' ) ) {
					uni.navigateTo( {
						url: "/pages/login/index"
					} )
					return
				}
				//收货地址setStorage 
				var that = this
				//判断座机或者手机
				var mo = /^(\d{3,4}\-)?\d{7,8}$/i;
				var mob = /^1[3457896]\d{9}$/i;
				var zuoji = mo.test( e.detail.value.mobile );
				var shouji = mob.test( e.detail.value.mobile );

				if ( e.detail.value.name == '' ) {
					wx.showToast( {
						title: '收件姓名不能为空',
						icon: 'error',
						duration: 2000
					} )
					return
				}
				if ( e.detail.value.mobile == '' ) {
					wx.showToast( {
						title: '收件电话不能为空',
						icon: 'error',
						duration: 2000
					} )
					return
				}

				if ( e.detail.value.detail == '' ) {
					wx.showToast( {
						title: '详细地址不能为空',
						icon: 'error',
						duration: 2000
					} )
					return
				}
				if ( this.group_name == '请选择收货地址' ) {
					wx.showToast( {
						title: '请选择收货地址',
						icon: 'error',
						duration: 2000
					} )
					return
				}
				if ( zuoji || shouji ) {
					uni.showLoading( {
						title: '加载中'
					} );
					var defaults = this.defaults
					var path = '/xyb/User_address/add';
					var data = {
						name: e.detail.value.name,
						mobile: e.detail.value.mobile,
						detail: e.detail.value.detail,
						group_name: this.group_name,
						defaults: defaults
					}
					ikz.post( path, data, ( res ) => {
						// console.log(res)
						if ( res.data.code ) {
							data.id = res.data.data.id;
							var address_all = app.globalData.user_address.address_all || [ ];
							if ( defaults == 'true' ) {
								if ( address_all.length > 0 ) {
									for ( var i = 0; i < address_all.length; i++ ) {
										if ( address_all[ i ].defaults == 'true' ) {
											address_all[ i ].defaults = 'false';
										}
									}
								}
								app.globalData.user_address.address_default = data;
								app.globalData.user_address.address_current = data;
							}
							address_all.push( data );
							app.globalData.user_address.address_all = address_all;
							wx.showToast( {
								title: '地址新增成功',
								icon: 'success',
								duration: 2000
							} );
							setTimeout( function( ) {
								uni.navigateBack( {
									delta: 1 // 回退前 delta(默认为1) 页面
								} );
								uni.hideLoading( )
							}, 2000 )
						} else {
							uni.showModal( {
								title: '提示',
								content: res.data.msg,
								showCancel: false,
								success: function( res ) {}
							} );
							uni.hideLoading( )
						}
					} );
				} else {
					wx.showModal( {
						title: '收件电话错误',
						content: '请填写正确的电话号码！',
						showCancel: false
					} )
				}
			},
			//点击 默认地址
			switchChange: function( e ) {
				console.log( e )
				this.defaults = e.detail.value
				this.$forceUpdate( );
			},
		},
	};
</script>

<style>
	page {
		background: #f2f2f2;
	}

	.main {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		/* width:100%; */
	}

	.ikz-add {
		width: 100%;
		margin: 10px auto;
		border-radius: 10rpx;
		background: #fff;
	}

	.moren {
		width: 750rpx;
		height: 96rpx;
		background: #FFFFFF;
		padding: 0 30rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.addRec-menu-o {
		font-family: Arial, Helvetica, sans-serif;
		font-size: 36rpx;
		border-bottom: 1px solid #eee;
	}

	.reciver-top {
		height: 70rpx;
		background-color: #2d90e5;
		text-align: center;
		color: white;
		line-height: 70rpx;
	}

	.addRec-menu-o-left {
		line-height: 100rpx;
		width: 20%;
	}

	.addRec-menu-o {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		padding: 20rpx;
		font-size: 30rpx;
		box-sizing: border-box;
	}

	.addRec-menu-o-right {
		width: 70%;
		color: gainsboro;
		font-size: 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 100rpx;

	}

	.addRec-menu-o-right input {
		width: 534rpx;
		height: 100rpx;
		color: black;
		background: #EDEFF2;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		padding: 0 26rpx;
		box-sizing: border-box;
	}

	.addRec-menu-o-right button {
		margin-left: 120rpx;
		background: #EDEFF2;
	}

	.bot-box {
		width: 750rpx;
		height: 176rpx;
		background: #FFFFFF;
		border-radius: 24rpx 24rpx 0rpx 0rpx;
		position: fixed;
		bottom: 0;
	}

	.ikz-add-address-btn {
		position: relative;
		width: 90%;
		margin-top: 14rpx;
		background: #247EFF;
		border: none;
		border-radius: 50px;
		color: #fff;
	}

	.ikz-cell-ft-in-access {
		width: 7px;
		height: 7px;
		border-top: 1px solid #828282;
		border-right: 1px solid #828282;
		transform: rotate(45deg);
	}

	.tips-txt {
		width: 750rpx;
		height: 260rpx;
		background: #FFFFFF;
		padding: 36rpx 30rpx;
		box-sizing: border-box;
		margin-top: 20rpx;
	}

	.top-txt {
		width: 100%;
		height: 44rpx;
		display: flex;
	}

	.sayming {
		width: 100%;
		height: 44rpx;
		font-weight: 400;
		font-size: 30rpx;
		color: #222222;
		text-align: left;
	}

	.center-txt {
		width: 352rpx;
		min-height: 34rpx;
		font-weight: 400;
		font-size: 22rpx;
		color: #FF4F21;
		text-align: left;
	}
</style>